#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

/*
 * ./button_led_test /dev/lzp_button0 /dev/lzp_led0
 */
int main(int argc, char **argv)
{
	int fd_button;
	int fd_led;
	char level;
	int status;
	
	/* 1. 判断参数 */
	if (argc != 3) 
	{
		return -1;
	}

	/* 2. 打开文件 */
	if ((fd_button = open(argv[1], O_RDWR)) == -1)
	{
		printf("can not open file %s\n", argv[1]);
		return -1;
	}
	if ((fd_led = open(argv[2], O_RDWR)) == -1)
        {
                printf("can not open file %s\n", argv[2]);
                return -1;
        }

	/* 3. 读文件 */
	while(1){
		read(fd_button, &level, 1);
		if(level == 0){
			//如果按键按下
			status = 1;
			write(fd_led, &status, 1);
		}else{
			//如果按键松开
                        status = 0;
                        write(fd_led, &status, 1);
		}
	}
	
	close(fd_led);
	close(fd_button);
	
	return 0;
}

